package com.example.administrator.szb.fragments.fragment_forTab;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.GZGLActivity;
import com.example.administrator.szb.activity.GZXYActivity;
import com.example.administrator.szb.activity.IdCardRzActivity;
import com.example.administrator.szb.activity.JFGLActivity;
import com.example.administrator.szb.activity.JGGLNOActivity;
import com.example.administrator.szb.activity.JGGLYESActivity;
import com.example.administrator.szb.activity.MessageActivity;
import com.example.administrator.szb.activity.MingPianActivity;
import com.example.administrator.szb.activity.MyCollectionActivity;
import com.example.administrator.szb.activity.MyZXActivity;
import com.example.administrator.szb.activity.SZActivity;
import com.example.administrator.szb.activity.WDGLNEWActivity;
import com.example.administrator.szb.activity.XMGLActivity;
import com.example.administrator.szb.activity.myyewu.YWGLNOActivity_new;
import com.example.administrator.szb.activity.myyewu.YWGLYESActivity;
import com.example.administrator.szb.adapter.AppBarStateChangeListener;
import com.example.administrator.szb.fragments.base.BaseFragment;
import com.example.administrator.szb.bean.UserBean;
import com.example.administrator.szb.config.FragmentsFlag;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.GetJsonDataUtil;
import com.example.administrator.szb.util.Toasts;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by D N on 2017/9/12.
 */

public class FragmentMyForNoRenZhen extends BaseFragment {
    View view;

    int isRz;//是否显示需要认证

    public static UserBean userBean;//用户个人中心标签
    @Bind(R.id.yh_image_icon)
    CircleImageView yhImageIcon;
    @Bind(R.id.my_yh_username)
    TextView myYhUsername;
    @Bind(R.id.my_text_js)
    TextView myTextJs;
    @Bind(R.id.my_unlogin_title)
    TextView myUnloginTitle;
    @Bind(R.id.my_unlogin_toolbar)
    Toolbar myUnloginToolbar;
    @Bind(R.id.my_unlogin_collapsingtoolbarlayout)
    CollapsingToolbarLayout myUnloginCollapsingtoolbarlayout;
    @Bind(R.id.my_unlogin_appbarlayout)
    AppBarLayout myUnloginAppbarlayout;
    @Bind(R.id.rz_info)
    TextView rzInfo;
    @Bind(R.id.my_unlogin_go_rz)
    TextView myUnloginGoRz;
    @Bind(R.id.my_jggl)
    LinearLayout myJggl;
    @Bind(R.id.my_jfgl)
    LinearLayout myJfgl;
    @Bind(R.id.my_xmgl)
    LinearLayout myXmgl;
    @Bind(R.id.my_wdgl)
    LinearLayout myWdgl;
    @Bind(R.id.my_unlogin_sc)
    LinearLayout myUnloginSc;
    @Bind(R.id.my_unlogin_zx)
    LinearLayout myUnloginZx;
    @Bind(R.id.my_unlogin_gz)
    LinearLayout myUnloginGz;
    @Bind(R.id.my_unlogin_yw)
    LinearLayout myUnloginYw;
    @Bind(R.id.my_unlogin_xx)
    LinearLayout myUnloginXx;
    @Bind(R.id.my_unlogin_gywm)
    LinearLayout myUnloginGywm;
    @Bind(R.id.my_unlogin_sz)
    LinearLayout myUnloginSz;
    @Bind(R.id.fragment_mine_swiperefreshLayout)
    SwipeRefreshLayout fragmentMineSwiperefreshLayout;
    @Bind(R.id.fragment_home_rz_isshow)
    LinearLayout fragmentHomeRzIsshow;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            isRz = bundle.getInt("isRz");
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine_unlogin, container, false);
        ButterKnife.bind(this, view);
        initListener();
        requestUserData();//获取用户信息
        //initView();
        initRefreshLayout();
        return view;
    }

    private void initRefreshLayout() {
        fragmentMineSwiperefreshLayout.setColorSchemeResources(R.color.zhutise);
        fragmentMineSwiperefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                fragmentMineSwiperefreshLayout.setRefreshing(true);
                requestUserData();
            }
        });

        fragmentMineSwiperefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                fragmentMineSwiperefreshLayout.setRefreshing(true);
                requestUserData();
            }
        });
    }

    private void initListener() {
        /**
         * 监听appBarLayout的滑动状态
         */
        myUnloginAppbarlayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                Log.d("STATE", state.name());
                if (state == State.EXPANDED) {
                    myUnloginTitle.setText("");
                    fragmentMineSwiperefreshLayout.setEnabled(true);
                    //展开状态

                } else if (state == State.COLLAPSED) {

                    //折叠状态
                    myUnloginTitle.setText("我的");
                    fragmentMineSwiperefreshLayout.setEnabled(false);

                } else {

                    //中间状态

                }
            }
        });
    }


    public void requestUserData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("id", SampleApplicationLike.getUserloginInstance().getId() + "");
        String json = GetJsonDataUtil.simpleMapToJsonStr(map);
        String url = URLAddress.URLBASE + URLAddress.URL_CENTER;
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, UserBean.class, URLAddress.URLBASE + URLAddress.URL_CENTER, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                userBean = (UserBean) response;
                if (userBean.getResult() == 1) {
                    initView();
                    fragmentMineSwiperefreshLayout.setRefreshing(false);
                    //myFramelayout.setVisibility(View.GONE);
                } else {
                    Toasts.show(getActivity(), "" + userBean.getErr_msg(), Toast.LENGTH_SHORT);
                }
                //response.toString();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.toString();
                fragmentMineSwiperefreshLayout.setRefreshing(false);
                Toasts.show(getActivity(), "数据异常", Toast.LENGTH_SHORT);
            }
        });
    }

    private void initView() {
        //userBean = SampleApplicationLike.getUserloginInstance();
        if (!TextUtils.isEmpty(userBean.getData().getName())) {
            myYhUsername.setText(userBean.getData().getName());
        } else {
            myYhUsername.setText("无名氏");
        }

        myTextJs.setText(userBean.getData().getCompany());
        if (!TextUtils.isEmpty(userBean.getData().getHeadimg())) {
            Glide.with(getActivity()).load(userBean.getData().getHeadimg()).error(R.drawable.mrtx).into(yhImageIcon);
        }

        switch (userBean.getData().getGenre()) {
            case 1:
                rzInfo.setText("您的账号尚未认证，认证后才能进行展示哦");
                myUnloginGoRz.setText("立即认证");
                break;
            case 2:
                rzInfo.setText("初级认证状态，认证层级越高权限排名越高哦!");
                myUnloginGoRz.setText("我要高级认证");
                break;
            case 3:
                rzInfo.setText("您尚未选择经营的业务，快去业务管理中设置吧！");
                myUnloginGoRz.setText("立即填写");
                break;
            case 4:
                rzInfo.setText("完整机构信息会增加上市公司的信任度哦");
                myUnloginGoRz.setText("立即填写");
                break;
            case 5:
                fragmentHomeRzIsshow.setVisibility(View.GONE);
                break;
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.yh_image_icon, R.id.my_yh_username, R.id.my_text_js, R.id.my_unlogin_title, R.id.my_unlogin_toolbar, R.id.my_unlogin_collapsingtoolbarlayout, R.id.my_unlogin_appbarlayout, R.id.my_unlogin_go_rz, R.id.my_jggl, R.id.my_jfgl, R.id.my_xmgl, R.id.my_wdgl, R.id.my_unlogin_xx, R.id.my_unlogin_gywm, R.id.my_unlogin_sz, R.id.my_unlogin_sc, R.id.my_unlogin_zx, R.id.my_unlogin_gz, R.id.my_unlogin_yw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.yh_image_icon:
                break;
            case R.id.my_yh_username:
                break;
            case R.id.my_text_js:
                break;
            case R.id.my_unlogin_title:
                break;
            case R.id.my_unlogin_toolbar:
                break;
            case R.id.my_unlogin_collapsingtoolbarlayout:
                break;
            case R.id.my_unlogin_appbarlayout:
                break;
            case R.id.my_unlogin_go_rz:
                if (userBean.getData().getGenre() == 1) {
                    startActivityForResult(new Intent(getActivity(), MingPianActivity.class), FragmentsFlag.FragmentMY_NO_RZ.ordinal());
                } else if (userBean.getData().getGenre() == 2) {
                    startActivityForResult(new Intent(getActivity(), IdCardRzActivity.class), FragmentsFlag.FragmentMY_NO_RZ.ordinal());
                }else if (userBean.getData().getGenre() == 3){
                    startActivity(new Intent(getActivity(), YWGLNOActivity_new.class));
                }else if(userBean.getData().getGenre() == 4){
                    startActivity(new Intent(getActivity(), JGGLNOActivity.class));
                }

                break;
            case R.id.my_jggl:
                /**
                 * todo flag 的值从登录状态中取
                 *
                 */

                if (userBean.getData().getGenre() <= 1){
                    DialogUtil.showToast(getActivity(),"初级认证通过后才可以完善机构信息哦");
                }else{
                    startActivity(new Intent(getActivity(), JGGLYESActivity.class));
                }



                break;
            case R.id.my_jfgl:
                startActivity(new Intent(getActivity(), JFGLActivity.class));
                break;
            case R.id.my_xmgl:
                startActivity(new Intent(getActivity(), XMGLActivity.class));
                break;
            case R.id.my_wdgl:
                startActivity(new Intent(getActivity(), WDGLNEWActivity.class));
                break;
            case R.id.my_unlogin_xx:
                //FragmentHome.messageCallBack.changeStatu();
                startActivity(new Intent(getActivity(), MessageActivity.class));
                break;
            case R.id.my_unlogin_gywm:
                Intent intent_web = new Intent(getActivity(),GZXYActivity.class);//额，
                intent_web.putExtra("title","关于我们");
                intent_web.putExtra("id",1);
                startActivity(intent_web);
                break;
            case R.id.my_unlogin_sz:
                startActivity(new Intent(getActivity(), SZActivity.class));
                break;
            case R.id.my_unlogin_sc:
                startActivity(new Intent(getActivity(), MyCollectionActivity.class));
                break;
            case R.id.my_unlogin_zx:

                if (userBean.getData().getGenre() <= 1) {
                    DialogUtil.showToast(getActivity(),"初级认证通过后才可以进行资讯管理哦");
                }else{
                    startActivity(new Intent(getActivity(), MyZXActivity.class));
                }


                break;
            case R.id.my_unlogin_gz:
                startActivity(new Intent(getActivity(), GZGLActivity.class));
                break;
            case R.id.my_unlogin_yw:
                /**
                 * todo flag 的值从登录状态中取
                 */

                if (userBean.getData().getGenre() <= 1) {
                    DialogUtil.showToast(getActivity(),"初级认证通过后才可以进行业务管理哦");
                }else{
                    int flag = userBean.getData().getGenre();
                    if (flag >= 2) {
                        startActivity(new Intent(getActivity(), YWGLYESActivity.class));
                    }else{
                        Toasts.show(getActivity()  , "认证后才可以选择业务管理哦", Toast.LENGTH_SHORT);
                    }
                }


                break;
        }
    }
}
